<?php
/**
 * Represents a javascript based confirm button.
 */
fily_XMLConverter::registerNamespace('confirmbutton', fily_ConfirmButton);
class fily_ConfirmButton implements fily_ChildControl, fily_XMLToControl {
    /**
     * question that appears
     * @var <type>
     */
    protected $question = 'Do you really want to continue?';

    /**
     * target control
     * @var  fily_Control
     */
    protected $targetControl;


    /**
     *
     */
    public function __construct() {
        $this->setExecutionTime(400);
    }

    /**
     * does actually nothing cause it's only javascript
     * @return string
     */
    public function toHtml() {
        // do nothing
        // it is only javascript
        return '';
    }

    /**
     * this control can't have a id
     * it returns a empty string
     * @return string
     */
    public function getId() {
        return '';
    }



    /**
     * parent from this control
     * @var fily_ControlToHtml
     */
    protected $parent;
    /**
     * returns the parent from this control
     * @return fily_ControlToHtml
     */
    public function getParent() {
        return $this->parent;
    }

    /**
     * set the parent from this control
     * @param fily_ControlToHtml $parent
     */
    public function setParent(fily_ControlToHtml $parent = null) {
        $this->parent = $parent;
    }

    /**
     * shortcut for toHtml()
     */
    public function __toString() {
        return $this->toHtml();
    }



    /**
     * prepares the code for the html output
     */
    public function prepare() {
        if($this->getTargetControl()!=null) {
            $code = sprintf("firelynx.ConfirmButton('%s', '%s', %s);",  $this->getTargetControl()->getId(),  $this->getQuestion(),$this->getExecutionTime() );
            fily_ClientCodeManager::addCode($code);
            fily_ClientCodeManager::useId($this->getTargetControl()->getId());
        }

    }

    /**
     * set the question that appears
     * @param string $value
     */
    public function setQuestion($value) {
        $this->question = $value;
    }

    /**
     * returns the question that appears
     * @return string
     */
    public function getQuestion() {
        return $this->question;
    }

    /**
     * set the target control
     * @param fily_Control $targetControl
     */
    public function setTargetControl(fily_Control $targetControl) {
        $this->targetControl = $targetControl;
    }

    /**
     * returns the target control
     * @return <type>
     */
    public function getTargetControl() {
        return $this->targetControl;
    }

         /**
     * execution time for the javascript code
     * @var int
     */
    protected $executionTime;

    /**
     * get the execution time for the javascript code
     * @return <type>
     */
    public function getExecutionTime() {
        return $this->executionTime;
    }

    /**
     *  set the execution time for the javascript code
     * @param <type> $executionZone
     */
    public function setExecutionTime($executionTime) {
        $this->executionTime = $executionTime;
    }
    /**
     * system id
     * @var string
     */
    protected $sid;

    /**
     * system id
     * @return string
     */
    public function getSid(){
        return $this->sid;
    }

    /**
     * system id
     * @param string $sid
     */
    public function setSid($sid){
        $this->sid = $sid;
    }



    /**
     * finds a control with the specific id
     * @param string $id
     * @return fily_ControlToHtml
     */
    public function getControlById($id) {
        return null;
    }


    /**
     * finds a control with the specific system id
     * @param string $sid
     * @return fily_ControlToHtml
     */
    public function getControlBySid($sid) {
        return null;
    }


    protected $lateBindingObjects = array();

    public function XMLAddControl(fily_ControlToHtml $control) {
        // ignore
    }
    public function XMLAttributes(array $attributes, $innerValue) {
        foreach($attributes as $key => $value) {
            switch ($key) {
                case 'targetcontrol':
                    $this->lateBindingObjects[$key] = $value;
                    break;
                case 'question':
                    $this->setQuestion($value);
                    break;
                case 'executionzone':
                    $this->setExecutionZone($value);
                    break;
               default:
                    throw new Exception('XML Convert : attribute "'. $key.'" not know for ' .  get_class($this));
                    break;
            }
        }
        if(!isset($attributes['question'])&& !empty($innerValue)) {
            $this->setQuestion($innerValue);
        }
    }
    public function XMLCallFunction($name, array $parameters, $innerValue,  fily_ControlToHtml $innerControl = null) {
    }
    public function XMLLateBinding(fily_ControlToHtml $baseControl) {
        foreach($this->lateBindingObjects as $key => $value) {
            switch ($key) {
                case 'targetcontrol':
                   $control = fily_XMLConverter::findControl($value, $this, $baseControl);

                    if($control!=null) {
                        $this->setTargetControl($control);
                    }
                    break;
            }
        }
        $this->lateBindingObjects = array();
    }


    public function find($expression) {
        $control = null;
        if(!fily_ExpressionHelper::isExpression($expression)) {
            //expression is an id
            $control = $this->getControlById($expression);
        }
        else {
            $expressionParts = fily_ExpressionHelper::getParts($expression) ;
            // first expression part is always empty
            if(count($expressionParts)>1) {
                switch ($expressionParts[1]) {
                    case 'next':
                        $control =  fily_ExpressionHelper::next($expressionParts, $expression, $this);
                        break;
                    case 'previous':
                        $control = fily_ExpressionHelper::previous($expressionParts, $expression, $this);
                        break;
                    case 'parent':
                        $control = fily_ExpressionHelper::parent($expressionParts, $expression, $this);
                        break;
                }
            }
            // found a control and have another expressions
            if($control!=null && !empty($expression)) {
                $control = $control->find($expression);
            }
        }
        return $control;
    }


}

?>
